package com.wlstock.funi.ui;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.fourmob.datetimepicker.date.DatePickerDialog;
import com.sleepbot.datetimepicker.time.RadialPickerLayout;
import com.sleepbot.datetimepicker.time.TimePickerDialog;
import com.wlstock.funi.R;
import com.wlstock.funi.model.SelfFundSetModel;
import com.wlstock.funi.utils.StrUtils;
import com.wlstock.funi.utils.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 16/7/3.
 */

public class QuickSet extends LinearLayout implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener, View.OnClickListener {

    private final Context mContext;

    private final EditText ed_tem_specific_price;
    private EditText ed_total_money;
    private Spinner spiner_price_tpye;
    private EditText ed_price_offset;
    private TextView tv_tem_begin_date;
    private TextView tv_tem_end_date;
    private TextView tv_tem_begin_time;
    private TextView tv_tem_end_time;
    private CheckBox cb_tem_quick_buy;
    public  okListener okListener;

    private DatePickerDialog dayPicker;
    private TimePickerDialog timePicker;

    private View curView;

    public static int TYPE_NONE = 0;
    public static int TYPE_OPEN = 1;
    public static int TYPE_HIGH = 2;
    public static int TYPE_LOW = 3;
    public static int TYPE_CLOSE = 4;
    public static int TYPE_CUR = 5;
    private SimpleDateFormat dateFormat;
    private SimpleDateFormat timeFormat;


    public QuickSet(Context context) {
        super(context);
        this.mContext = context;
        View v = View.inflate(context, R.layout.pop_quick_set, this);
        ed_tem_specific_price = (EditText)v.findViewById(R.id.ed_tem_specific_price);
        ed_total_money = (EditText)v.findViewById(R.id.ed_total_money);
        spiner_price_tpye = (Spinner) v.findViewById(R.id.spiner_price_tpye);
        ed_price_offset = (EditText) v.findViewById(R.id.ed_price_offset);
        tv_tem_begin_date = (TextView) v.findViewById(R.id.tv_tem_begin_date);
        tv_tem_end_date   = (TextView) v.findViewById(R.id.tv_tem_end_date);
        tv_tem_begin_time = (TextView) v.findViewById(R.id.tv_tem_begin_time);
        tv_tem_end_time   = (TextView) v.findViewById(R.id.tv_tem_end_time);
        cb_tem_quick_buy  = (CheckBox) v.findViewById(R.id.cb_tem_quick_buy);

        Calendar calInstance = Calendar.getInstance();
        dayPicker = DatePickerDialog.newInstance(this, calInstance.get(Calendar.YEAR), calInstance.get(Calendar.MONTH), calInstance.get(Calendar.DAY_OF_MONTH));
        timePicker = TimePickerDialog.newInstance(this, calInstance.get(Calendar.HOUR_OF_DAY), calInstance.get(Calendar.MINUTE), true);

        tv_tem_begin_date.setOnClickListener(this);
        tv_tem_end_date.setOnClickListener(this);
        tv_tem_begin_time.setOnClickListener(this);
        tv_tem_end_time.setOnClickListener(this);

        dateFormat = new SimpleDateFormat("MM-dd");
        timeFormat = new SimpleDateFormat("hh:mm:ss");
        tv_tem_begin_date.setText(dateFormat.format(new Date()));
        tv_tem_end_date.setText(dateFormat.format(TimeUtils.getDateByDayOffset(new Date(),10)));
    }

    public void getValue() throws ParseException {
        if (okListener!=null){
            if (!StrUtils.isNullOrEmpty(ed_tem_specific_price.getText().toString())){
                String specificPrice = ed_tem_specific_price.getText().toString();
                String totalMoney = ed_total_money.getText().toString();
                String beginDate = tv_tem_begin_date.getText().toString();
                String endDate =tv_tem_end_date.getText().toString();
                String beginTime = tv_tem_begin_time.getText().toString();
                String endTime = tv_tem_end_time.getText().toString();

                int type = spiner_price_tpye.getSelectedItemPosition();
                String offset = ed_price_offset.getText().toString();


                if (isCorrect(type, offset, totalMoney, beginDate, endDate, beginTime, endTime)){
                    okListener.onOK(type,Float.valueOf(offset), Long.valueOf(totalMoney),dateFormat.parse(beginDate),dateFormat.parse(endDate),beginTime,endTime,cb_tem_quick_buy.isChecked());
                }else{
                    okListener.onError();
                }
            }

        }
    }

    private boolean isCorrect(int type, String offset, String totalMoney, String beginDate, String endDate, String beginTime, String endTime) {
        return type!=0&&!StrUtils.isNullOrEmpty(offset)&&!StrUtils.isNullOrEmpty(totalMoney)&&!StrUtils.isNullOrEmpty(beginDate)&&!StrUtils.isNullOrEmpty(endDate)&&!StrUtils.isNullOrEmpty(beginTime)&&!StrUtils.isNullOrEmpty(endTime);
    }

    private boolean isCorrect(String specificPrice, String totalMoney, String beginDate, String endDate, String beginTime, String endTime) {
        return !StrUtils.isNullOrEmpty(specificPrice)&&!StrUtils.isNullOrEmpty(totalMoney)&&!StrUtils.isNullOrEmpty(beginDate)&&!StrUtils.isNullOrEmpty(endDate)&&!StrUtils.isNullOrEmpty(beginTime)&&!StrUtils.isNullOrEmpty(endTime);
    }

    @Override
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
        month = month + 1;
        String select = month+"-"+day;
        switch (curView.getId()){
            case R.id.tv_tem_begin_date:
                tv_tem_begin_date.setText(select);
                break;
            case R.id.tv_tem_end_date:
                tv_tem_end_date.setText(select);
                break;
        }

    }

    @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        SelfFundSetModel model = (SelfFundSetModel) curView.getTag();
        String select = hourOfDay + ":" + minute;
        ((TextView)curView).setText(select);
    }

    @Override
    public void onClick(View v) {
        curView = v;
        switch (v.getId()){
            case R.id.tv_tem_begin_date:
            case R.id.tv_tem_end_date:
                dayPicker.setCloseOnSingleTapDay(true);
                dayPicker.setYearRange(2014, 2017);
                if (!dayPicker.isAdded()) {
                    dayPicker.show(((FragmentActivity)mContext).getSupportFragmentManager(), "DAY_PICKER");
                }
                break;
            case R.id.tv_tem_begin_time:
            case R.id.tv_tem_end_time:
                timePicker.setCloseOnSingleTapMinute(true);
                if (!timePicker.isAdded()) {
                    timePicker.show(((FragmentActivity)mContext).getSupportFragmentManager(), "TIME_PICKER");
                }
                break;
        }
    }

    public interface okListener{
        public void  onOK(float specifiPrice, long totalPrice, Date beginDate, Date endDate, String beginTime, String endTime,boolean isQuickBuy);
        public void  onOK(int type,float offset,long totalPrice, Date beginDate, Date endDate, String beginTime, String endTime,boolean isQuickBuy);
        public void  onError();
    }


}
